package com.ysq.tankgame.tankValue;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

import com.ysq.tankgame.EnteryTank;

public class Recorder {
    private static int AllenemyTankNum = 0;
    private static BufferedWriter bw = null;
    private static BufferedReader br = null;
    private static FileReader fr = null;
    private static FileWriter fw = null;
    private static String recordFile = "src/com/ysq/tankgame/Value.txt";
    private static Vector<EnteryTank> eTanks = new Vector<>();
    private static Vector<Node> nodes = new Vector<>();

    public static void seteTanks(Vector<EnteryTank> eTanks) {
        Recorder.eTanks = eTanks;
    }

    public static int getAllenemyTankNum() {
        return AllenemyTankNum;
    }

    public static void setAllenemyTankNum(int allenemyTankNum) {
        AllenemyTankNum = allenemyTankNum;
    }

    /**
     * 被击毁，坦克数量加一
     */
    public static void record() {
        AllenemyTankNum++;
    }

    /**
     * 保存游戏信息
     */
    public static void save() {
        try {
            bw = new BufferedWriter(new FileWriter(recordFile));
            bw.write(AllenemyTankNum + "\r\n");
            for (int i = 0; i < eTanks.size(); i++) {
                EnteryTank enteryTank = eTanks.get(i);
                if (enteryTank.isLive()) { // 如果存活
                    String UpValue = enteryTank.getX() + " " + enteryTank.getY() + " " + enteryTank.getDirect();
                    bw.write(UpValue + "\r\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 加载信息
     */
    public static Vector<Node> load() {
        try {
            br = new BufferedReader(new FileReader(recordFile));
            AllenemyTankNum = Integer.parseInt(br.readLine());
            String line = "";
            while ((line = br.readLine()) != null) {
                String[] data = line.split(" ");
                nodes.add(new Node(Integer.parseInt(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2])));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return nodes;
    }
}
